/**
 * 
 */
package info.niwota.search;

import java.util.Random;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

/**
 * @author qiangli
 *
 */
public class SearchEngine {
	public int id;
	public String name;
	public String uri;

	public SearchEngine() {
	}
	
	public SearchEngine(int id, String name, String uri) {
		this.id = id;
		this.name = name;
		this.uri = uri;
	}
	
	private static final SearchEngine[] engines = {
		new SearchEngine(101, "Bing", "http://www.bing.com/search?q="),
		new SearchEngine(102, "Google", "http://www.google.com/search?q="),
		new SearchEngine(103, "Yahoo!", "http://search.yahoo.com/search?q="),
		new SearchEngine(104, "Dictionary.com", "http://dictionary.reference.com/search?q="),
		new SearchEngine(105, "Merriam-Webster Online", "http://m-w.com/cgi-bin/dictionary?va="),
		new SearchEngine(106, "OneLook Dictionary", "http://onelook.com/?w="),
		new SearchEngine(107, "Wikipedia", "http://en.wikipedia.org/wiki/Special:Search?search="),
		new SearchEngine(108, "Google Images", "http://images.google.com/images?q="),
		new SearchEngine(109, "Google Maps", "http://maps.google.com/maps?q="),
		new SearchEngine(110, "Google News", "http://news.google.com/news/search?q="),
		new SearchEngine(111, "Google Products", "http://www.google.com/products?q="),
		new SearchEngine(112, "Google Videos", "http://video.google.com/videosearch?q="),
		new SearchEngine(113, "YouTube", "http://www.youtube.com/results?q="),
		new SearchEngine(114, "Babelfish (English-Chinese)", "http://babelfish.yahoo.com/translate_txt?lp=en_zh&amp;trtext="),
		new SearchEngine(115, "Google Translate (English-Chinese)", "http://translate.google.com/?sl=en&amp;tl=zh-CN&amp;text="),
	};

	/**
	 * 
	 * @return a shuffled list of search engines
	 */
	public static final SearchEngine[] getEngines() {
		final int len = engines.length;
		SearchEngine[] my = new SearchEngine[len];
		for (int i = 0; i < len; i++) {
			my[i] = engines[i];
		}
		
		//shuffle
		Random rand = new Random();
		for (int i = 0; i < len; i++) {
			int j = rand.nextInt(len);
			SearchEngine se = my[i];
			my[i] = my[j];
			my[j] = se;
		}
		
		return my;
	}
	
	public static JSONObject toJson(SearchEngine[] obj) throws JSONException {
		JSONObject json = new JSONObject();
		JSONArray names = new JSONArray();
		for (int i = 0; i < obj.length; i++) {
			names.put(obj[i].name);
		}
		json.put("names", names);
		return json;
	}
}
